/*
 * player.c
 *
 *  Created on: 2010-9-30
 *      Author: jack
 */

#include <gtk-2.0/gtk/gtk.h>
#include <libglade-2.0/glade/glade.h>
#include <glib-2.0/glib.h>
#include <stdlib.h>
#include <string.h>
#include "../include/gstreamer.h"
#include "../include/xml.h"
#include "../include/structs.h"


static GtkBuilder * builder=NULL;
static GtkWidget * mainmenu,*play_button,*pause_button,*stop_button,*exit_button,*artist_content,*title_content,*prev_button,*next_button,*time_label,*lyric_label1,*lyric_label2,*lyric_label3;
static GtkAdjustment *volume_adjustment;/*GtkAdjustment并不继承于GtkWidget而是直接从GtkObject继承!!!*/
static unsigned int time_handler,lyric_handler;
lyric *lyric_head=NULL;
char *lyric1,*lyric2,*lyric3;



int get_lyric_xml()
{
	lyric_head=NULL;
	char query[100];/*TODO:size?*/
	sprintf(query,"pw=Shit&action=lyric&id=%s",get_song_id());
	lyric_head=get_lyric(query);
	return 0;
}

static int gtk_quit_cb(GtkWidget *widget,gpointer data)
{
	gtk_timeout_remove(time_handler);/*delete time-getting task*/
	gtk_timeout_remove(lyric_handler);/*delete lyric-showing task*/
	destory_pipeline();  /*退出播放器前销毁pipeline的数据*/
	gtk_widget_destroy(mainmenu);

	return 0;
}

static int play_cb(GtkWidget *widget,gpointer data)
{
	play();
	return 0;
}

static int pause_cb(GtkWidget *widget,gpointer data)
{
	pause();
	return 0;
}

static int stop_cb(GtkWidget *widget,gpointer data)
{
	stop();
	return 0;
}

static int prev_cb(GtkWidget *widget,gpointer data)
{
	prev();
	return 0;
}

static int next_cb(GtkWidget *widget,gpointer data)
{
	next();
	return 0;
}
static int volume_changed_cb(GtkWidget *widget,gpointer data)
{
	set_volume(gtk_adjustment_get_value(volume_adjustment));
	return 0;
}

static int show_time_cb()
{
	char formated_time[300]=" ";
	if(get_time(formated_time,NULL)==0)
	{
		gtk_label_set_text(time_label,formated_time);
	}
}

static int compare_lyric_time(char *play_time,char *lyric_time)
{
	int play_minute,play_second,play_milsecond,i;
	int lyric_minute,lyric_second,lyric_milsecond;
	char play_temp[4];
	char lyric_temp[4];
	for(i=3;i<4;i++)
	{
		play_temp[i-3]=play_time[i];
	}
	play_temp[i-3]='\0';
	for(i=0;i<2;i++)
	{
		lyric_temp[i]=lyric_time[i];
	}
	lyric_temp[i]='\0';
	play_minute=atoi(play_temp);
	lyric_minute=atoi(lyric_temp);
	if(play_minute>lyric_minute)
	{
		return 1;
	}
	else
	{
		for(i=5;i<7;i++)
		{
			play_temp[i-5]=play_time[i];
		}
		play_temp[i-5]='\0';
		for(i=3;i<5;i++)
		{
			lyric_temp[i-3]=lyric_time[i];
		}
		lyric_temp[i-3]='\0';
		play_second=atoi(play_temp);
		lyric_second=atoi(lyric_temp);
		if(play_second>lyric_second)
		{
			return 1;
		}
		else
		{
			for(i=8;i<10;i++)
			{
				play_temp[i-8]=play_time[i];
			}
			play_temp[i-8]='\0';
			for(i=6;i<8;i++)
			{
				lyric_temp[i-6]=lyric_time[i];
			}
			lyric_temp[i-6]='\0';
			play_milsecond=atoi(play_temp);
			lyric_milsecond=atoi(lyric_temp);
			if(play_milsecond>lyric_milsecond)
			{
				return 1;
			}
			else
			{
				return -1;
			}
		}
	}
}

static int format_lyric(char *lyric_time,char *lyric1,char *lyric2,char *lyric3)
{
	strcpy(lyric1,"");
	strcpy(lyric2,"");
	strcpy(lyric3,"");
	if(lyric_head!=NULL)
	{
		lyric * current=(lyric *)malloc(sizeof(lyric));
		current=lyric_head;
		while(current!=NULL)
		{
			char play_time[20]="";
			get_time(play_time,NULL);
			if(compare_lyric_time(play_time,current->time)>0)
			{
				current=current->next;
			}
			else
			{
				break;
			}
		}
		if(current!=NULL)
		{
			sprintf(lyric2,"%s",current->lyric);
			if(current->prev!=NULL)
			{
				sprintf(lyric1,"%s",current->prev->lyric);
			}
			else
			{
				sprintf(lyric1,"");
			}
			if(current->next!=NULL)
			{
				sprintf(lyric3,"%s",current->next->lyric);
			}
			else
			{
				sprintf(lyric3,"");
			}
		}
		else
		{
			sprintf(lyric2,"");
		}
		return 0;
	}
	else
	{
		return -1;
	}
}

static int show_lyric_cb()
{
	char time[20]="";
	if(lyric1!=NULL&&lyric2!=NULL&&lyric3!=NULL)
	{
		free(lyric1),lyric1=NULL;
		free(lyric2),lyric2=NULL;
		free(lyric3),lyric3=NULL;
	}
	lyric1=(char *)malloc(150*sizeof(char));
	lyric2=(char *)malloc(150*sizeof(char));
	lyric3=(char *)malloc(150*sizeof(char));
	if(get_time(NULL,time)==0)
	{
		if(format_lyric(time,lyric1,lyric2,lyric3)==0)
		{
			gtk_label_set_text(lyric_label1,lyric1);
			gtk_label_set_text(lyric_label2,lyric2);
			gtk_label_set_text(lyric_label3,lyric3);
		}
	}
}

int change_info(songinfo *info)
{
	gtk_label_set_text(artist_content,info->artist);
	gtk_label_set_text(title_content,info->title);
	return 0;
}

int start_player(songinfo *head)
{
	builder = gtk_builder_new();
	gtk_builder_add_from_file(builder, "../glade/player.glade", NULL);
	mainmenu = GTK_WIDGET(gtk_builder_get_object(builder, "player"));
	play_button=GTK_WIDGET(gtk_builder_get_object(builder, "play_button"));
	pause_button=GTK_WIDGET(gtk_builder_get_object(builder, "pause_button"));
	stop_button=GTK_WIDGET(gtk_builder_get_object(builder, "stop_button"));
	exit_button=GTK_WIDGET(gtk_builder_get_object(builder, "exit_button"));
	title_content=GTK_WIDGET(gtk_builder_get_object(builder, "title_content"));
	artist_content=GTK_WIDGET(gtk_builder_get_object(builder, "artist_content"));
	prev_button=GTK_WIDGET(gtk_builder_get_object(builder, "prev_button"));
	next_button=GTK_WIDGET(gtk_builder_get_object(builder, "next_button"));
	time_label=GTK_WIDGET(gtk_builder_get_object(builder, "time_label"));
	lyric_label1=GTK_WIDGET(gtk_builder_get_object(builder, "lyric_label1"));
	lyric_label2=GTK_WIDGET(gtk_builder_get_object(builder, "lyric_label2"));
	lyric_label3=GTK_WIDGET(gtk_builder_get_object(builder, "lyric_label3"));
	volume_adjustment=(GtkAdjustment *)gtk_builder_get_object(builder, "volume_adjustment");
	g_signal_connect( G_OBJECT(mainmenu), "destroy",G_CALLBACK(gtk_quit_cb),NULL);
	g_signal_connect( G_OBJECT(play_button), "clicked",G_CALLBACK(play_cb),NULL);
	g_signal_connect( G_OBJECT(pause_button), "clicked",G_CALLBACK(pause_cb),NULL);
	g_signal_connect( G_OBJECT(stop_button), "clicked",G_CALLBACK(stop_cb),NULL);
	g_signal_connect( G_OBJECT(exit_button), "clicked",G_CALLBACK(gtk_quit_cb),NULL);
	g_signal_connect( G_OBJECT(prev_button), "clicked",G_CALLBACK(prev_cb),NULL);
	g_signal_connect( G_OBJECT(next_button), "clicked",G_CALLBACK(next_cb),NULL);
	g_signal_connect( G_OBJECT(volume_adjustment), "value-changed",G_CALLBACK(volume_changed_cb),NULL);
	gtk_builder_connect_signals(builder, NULL);
	change_info(head->next);  /*get initial song info*/
	gtk_adjustment_set_value(volume_adjustment,get_volume());/*get initial volume*/
	gtk_widget_show(mainmenu);
	time_handler=g_timeout_add(100,show_time_cb,NULL);/*add time-getting task*/
	lyric_handler=g_timeout_add(100,show_lyric_cb,NULL);/*add time-getting task
	/*g_object_unref(G_OBJECT(builder));*/
	return 0;
}
